<?php
 
require_once 'class.account.php';

class character extends account {
	
	public function characterRegistry($id)
	{
		
		$rd = $this -> setData("SELECT c.character_name AS character_name, c.character_surname AS character_surname, IF(c.character_sex = 1,'M','F') AS character_sex, r.race_name AS character_race, c.character_created AS character_created, c.character_lasttime AS character_lasttime
				FROM characters c LEFT JOIN races r ON c.character_race = r.race_id WHERE character_id = $id") -> getData();
		
		return $rd;
		
	}
	
	public function characterRegistryAll()
	{
	
		$this -> query("SELECT c.character_name AS character_name, c.character_surname AS character_surname, IF(c.character_sex = 1,'M','F') AS character_sex, r.race_name AS character_race, c.character_created AS character_created, c.character_lasttime AS character_lasttime
			FROM characters c LEFT JOIN races r ON c.character_race = r.race_id ORDER BY c.character_name");
		
		if($this -> numrows > 0)
		{
			$rd_all = array();
			
			while($rd = $this -> fetch())
			{
				$rd_all[] = $rd;
			}
			
			return $rd_all;
		} else {
			return FALSE;
		}
	
	}
	
	public function characterOnlineList()
	{
		
		$this -> query("SELECT character_id, character_name, character_surname, g.guild_name AS character_guild, gr.rank_name AS character_guildrank, gr.rank_image AS rank_img, r.race_name AS character_race, r.race_image AS race_img,  IF(c.character_sex = 1,'M','F') AS character_sex, l.loc_name AS character_loc 
				FROM characters c
				LEFT JOIN races r ON c.character_race = r.race_id
				LEFT JOIN guilds g ON c.character_guild = g.guild_id
				LEFT JOIN guildranks gr ON c.character_guild = gr.guild_id AND c.character_guildrank = gr.rank_id
				LEFT JOIN locations l ON c.character_loc = l.loc_id
				WHERE character_lasttime > (NOW() - INTERVAL 1 MINUTE) ORDER BY character_name ASC;");
		
		if($this -> numrows > 0)
		{			
			$list = '<table class="table table-striped table-bordered"><tbody>';
			while($rd = $this -> fetch()) {
				
				$character_id 		= trim(htmlentities($rd['character_id']));
				$character_name 	= trim(htmlentities($rd['character_name']));
				$character_surname 	= trim(htmlentities($rd['character_surname']));
				$character_loc 		= trim(htmlentities($rd['character_loc']));
				$guild_name 		= trim(htmlentities($rd['character_guild']));
				$race_name 			= trim(htmlentities($rd['character_race']));
				$race_img 			= trim(htmlentities($rd['race_img']));
				$rank_name			= trim(htmlentities($rd['character_guildrank']));
				$rank_img 			= trim(htmlentities($rd['rank_img']));
				$loc_name 			= trim(htmlentities($rd['character_loc']));
				
				$list .= '<tr><td><a href="profile.php?id=' . $character_id . '" target="_self">
				' . $character_name . ' ' . $character_surname . '</a></td>
				<td><img src="img/races/' . $race_img . '" width="20px" height="20px" border="0" title="' . $race_name . '" alt="' . $race_name . '"></td>
				<td><img src="img/guilds/' . $rank_img . '" width="20px" height="20px" border="0" title="' . $rank_name . ' di ' . $guild_name . '" alt="' . $guild_name . '"></td>
				<td><a href="game.php?loc=' . $character_loc . '" target="_top">' . $loc_name . '</a></td></tr>';
			}
			$list .= '</tbody></table>';
			
				if( ! empty($_SESSION['cid']))
				{
					$this -> query('UPDATE characters SET character_lasttime = NOW() WHERE character_id = ' . $_SESSION['cid']);
				}
			
			return trim($list);
		} else {
			return FALSE;
		}
	}

	public function updateLocationCharacter($loc_id) 
	{
		$loc_id = ( ! empty($loc_id)) ? $loc_id : 1;
		$uid	= $_SESSION['uid'];
		
		$this -> query("UPDATE users SET user_lasttime = NOW() WHERE user_id = '$uid'");
		
	}
	
}

?>